<?php
SQLvalidate($_GET['cat']);
SQLvalidate($_GET['id']);
SQLvalidate($_GET['rec']);

if(!is_numeric($_GET['rec']))
$_GET['rec']=0;

//dodanie intra
if($GLOBALS['lang']['news']['intro'] && !$_GET['id'] && !$_GET['action'])
$intro=string_template(read_file('intro.php'), array('theme_path'=>$GLOBALS['theme_path'], 'description'=>$GLOBALS['lang']['news']['intro_title'], 'content'=>read_text($GLOBALS['lang']['news']['intro'])));
//

$db = new dbquery;

if($_GET['id']) {

	//selecting record from news table
	$db->query("SELECT * FROM $conf[prefix]news WHERE id=$_GET[id]") or $db->err(__FILE__, __LINE__);
	$d=$db->fetch_object();
	//

	//if doesn't exists
	if($db->num_rows()==0 or !$d->visible) {
		redirect('index.php');
		exit;
	}
	//

	$ttl = "title_".$_SESSION['lang_short'];
	$txt = "text_".$_SESSION['lang_short'];

	if($d->$ttl == '' or $d->$txt == '') {
		$ttl = "title_".$conf['language'];
		$txt = "text_".$conf['language'];
	}

	$title = read_text_rest($d->$ttl);

	if(strpos($d->$txt, '[end]')!==false)
		$text = explode('[end]', $d->$txt);
	else {
		if(($pos = strpos($d->$txt, "\r\n\r\n"))!==false) {
			$text[0] = substr($d->$txt, 0, $pos);
			$text[1] = substr($d->$txt, $pos+1); 
		} else $text = explode('[end]', $d->$txt);
	}

	//site_title
	$GLOBALS['lang']['global']['site_title']=read_text_rest($title).' - '.$GLOBALS['lang']['global']['site_title'];
	$GLOBALS['lang']['global']['site_description']=read_text_rest(strip_exbcode(add_dots_($text[0], 100))).' - '.$GLOBALS['lang']['global']['site_description'];

	$pages = count($text)-1;

	if($_GET['rec']!=0) {
		$text_begin = '';
		$text = $text[$_GET['rec']+1];
	}
	else {
		$text_begin = $text[0];
		$text = $text[1];
	}

	$d->source_url=read_text_rest($d->source_url);
	$d->source=read_text_rest($d->source);

	//source
	if($d->source && $d->source_url)
		$source='<a href="'.$d->source_url.'" target="_blank">'.$d->source.'</a>';
	//

	//page_link
	$_SERVER['REQUEST_URI']=eregi_replace("\&rec_c\=[0-9]+", '', $_SERVER['REQUEST_URI']);
	$_SERVER['REQUEST_URI']=eregi_replace("\&rec\=[0-9]+", '', $_SERVER['REQUEST_URI']);
	$page_link=split_to_pages('<a href="'.$_SERVER['REQUEST_URI'].'&amp;rec={rec}">{nr}</a>', 1, $pages, $_GET['rec']);
	//

	//adding {edit} & {delete} if user is admin
	if(perms_check('news', 'edit'))
		$edit='<a href="index.php?module=admin&amp;action=news&amp;cmd=edit&amp;id='.$d->id.'">'.$GLOBALS['lang']['global']['edit'].'</a>';
	if(perms_check('news', 'del'))
		$delete='<a href="index.php?module=admin&amp;action=news&amp;cmd=really_delete&amp;id='.$d->id.'">'.$GLOBALS['lang']['global']['delete'].'</a>';
	//

	//current category
	if($d->cat) {
		$cur_cat_name='';
		$cur_cat_desc='';

		$cat=get_cat_by_id($d->cat);

		$t = "desc_$_SESSION[lang_short]";
		$tl = "name_$_SESSION[lang_short]";

		if($cat->$t == '' && $cat->$tl == '') {
			$t = "desc_$conf[language]";
			$tl = "name_$conf[language]";
		}

		$cur_cat_name=read_text_rest($cat->$tl);
		$cur_cat_desc=read_text($cat->$t);	
	}

	if($conf['mod_rewrite'])
		$cur_cat_url='news-'.read_text_mr($cat->$tl).'_'.$cat->id.'.html';
	else
		$cur_cat_url='index.php?module=news&amp;cat='.$d->cat;
	//

	$cmts='comments_'.$_SESSION['lang_short'];

	$array=array('date' => date($conf['date_format'], strtotime($d->date)), 'theme_path' => $GLOBALS['theme_path'], 'source' => $source, 'comments' => string_template($GLOBALS['lang']['comments'], array('comments' => $d->$cmts, 'url' => $url)), 'id' => $d->id, 'cat' => $d->cat, 'author' => get_user_overlib_by_id($d->author), 'edit'=>$edit, 'delete'=>$delete, 'page_link'=>$page_link, 'cur_cat_name'=>$cur_cat_name, 'cur_cat_desc'=>$cur_cat_desc, 'cur_cat_url'=>$cur_cat_url, 'title' => $title, 'text' => read_text($text), 'text_begin' => read_text($text_begin));
	$content = string_template(read_file('news.php'), $array);

	if(is_module_installed('comments')) {
		require_once("includes/functions/comments.php");
		$content .= comments('news', $_GET['id']);
	}
}
elseif(!$_GET['id'] && !$_GET['action']) {
	if(is_numeric($_GET['cat']))
		$id_='&& cat IN ('.implode(', ', get_subcategories($_GET['cat'])).')';

				//selecting records from news table
				$db->query("SELECT COUNT(id) as rec FROM $conf[prefix]news WHERE visible='1' && text_$_SESSION[lang_short]!='' $id_") or $db->err(__FILE__, __LINE__);
				$q=$db->fetch_object();
				$q=$q->rec;

				$db->query("SELECT * FROM $conf[prefix]news WHERE visible='1' && text_$_SESSION[lang_short]!='' $id_ ORDER BY date DESC LIMIT $_GET[rec], $conf[news_archive]") or $db->err(__FILE__, __LINE__);

				//news pages
				$url=eregi_replace("\&amp;rec\=[0-9]+", '', $_SERVER['REQUEST_URI']);
				if(strpos($url, "?")===false) $url.="?";
				$page_link_news=split_to_pages('<a href="'.$url.'&amp;rec={rec}#news_scroll">{nr}</a>', $conf['news_archive'], $q, $_GET['rec']);
				//

				//site_title
				if($_GET['cat']) {
				$cat=get_cat_by_id($_GET['cat']);

				$t = "desc_$_SESSION[lang_short]";
				$tl = "name_$_SESSION[lang_short]";

				if($cat->$t == '' && $cat->$tl == '') {
					$t = "desc_$conf[language]";
					$tl = "name_$conf[language]";
				}

				$cur_cat_name=read_text_rest($cat->$tl);
				$cur_cat_desc=read_text($cat->$t);

				$GLOBALS['lang']['global']['site_title']=read_text_rest($cur_cat_name).' - '.$GLOBALS['lang']['global']['site_title'];
				$GLOBALS['lang']['global']['site_description']=read_text_rest($cur_cat_desc).' - '.$GLOBALS['lang']['global']['site_description'];
				}
				//

				$tmp=read_file('news_item.php');
				$i=0;
				$cnt = new tpl;

				while($d=$db->fetch_object()) {
					//wybieranie jezyka
					$ttl = "title_$_SESSION[lang_short]";
					$txt = "text_$_SESSION[lang_short]";

					if($d->$ttl == '' or $d->$txt == '') {
						$ttl = "title_$conf[language]";
						$txt = "text_$conf[language]";
					}

					$d->source_url=read_text_rest($d->source_url);
					$d->source=read_text_rest($d->source);
					$title = read_text_rest($d->$ttl);

					if($conf['mod_rewrite']) //jezeli wlaczony mod_rewrite
						$url='new-'.read_text_mr($title).'_'.$d->id.'.html';
					else
						$url='index.php?module=news&amp;id='.$d->id;

					$text = read_text(add_dots_($d->$txt, 200)).string_template($GLOBALS['lang']['global']['read_more'], array('theme_path'=>$GLOBALS['theme_path'], 'id'=>$d->id, 'url'=>$url));

					//sprawdzanie czy jest zrodlo
					$source='';
					if($d->source && $d->source_url)
						$source='<a href="'.$d->source_url.'" target="_blank">'.$d->source.'</a>';
					//

					//adding {edit} & {delete} if user is admin
					if(perms_check('news', 'edit'))
						$edit='<a href="index.php?module=admin&amp;action=news&amp;cmd=edit&amp;id='.$d->id.'">'.$GLOBALS['lang']['global']['edit'].'</a>';
					if(perms_check('news', 'del'))
						$delete='<a href="index.php?module=admin&amp;action=news&amp;cmd=really_delete&amp;id='.$d->id.'">'.$GLOBALS['lang']['global']['delete'].'</a>';
					//

					//current category
					$cur_cat_name='';
					$cur_cat_desc='';
					if($d->cat) {
						$cat=get_cat_by_id($d->cat);

						$t = "desc_$_SESSION[lang_short]";
						$tl = "name_$_SESSION[lang_short]";

						if($cat->$t == '' && $cat->$tl == '') {
							$t = "desc_$conf[language]";
							$tl = "name_$conf[language]";
						}

						$cur_cat_name=read_text_rest($cat->$tl);
						$cur_cat_desc=read_text($cat->$t);
					}

					if($conf['mod_rewrite'])
						$cur_cat_url='news-'.read_text_mr($cat->$tl).'_'.$cat->id.'.html';
					else
						$cur_cat_url='index.php?module=news&amp;cat='.$d->cat;
					//

					$cmts='comments_'.$_SESSION['lang_short'];

					$array=array('date' => date($conf['date_format'], strtotime($d->date)), 'theme_path' => $GLOBALS['theme_path'], 'source' => $source, 'comments' => string_template($GLOBALS['lang']['global']['comments'], array('comments' => $d->$cmts, 'url' => $url)), 'id' => $d->id, 'cat' => $d->cat, 'author' => get_user_overlib_by_id($d->author), 'edit'=>$edit, 'delete'=>$delete, 'cur_cat_name'=>$cur_cat_name, 'cur_cat_desc'=>$cur_cat_desc, 'cur_cat_url'=>$cur_cat_url, 'i'=>(($i%2)+1), 'url'=>$url, 'title' => $title, 'text' => $text);
					$cnt->create_string($tmp, $array);
					$cnt->parse_php();

					$i++;
				}
				$content=$cnt->get();
}
elseif($_GET['action'] == 'suggest') {
	//select author
	$db = new dbquery;
	$db->query("SELECT * FROM $conf[prefix_users]users WHERE id='$_SESSION[id]' or id=1") or $db->err(__FILE__, __LINE__);

	while($u=$db->fetch_object()) {
		if($u->id!=$_SESSION['id'])
			$select_author.='<OPTION VALUE="'.$u->id.'">'.$u->name.' '.$u->surname.' ['. $u->login .']</OPTION>';
		else
			$select_author.='<OPTION VALUE="'.$u->id.'" SELECTED>'.$u->name.' '.$u->surname.' ['. $u->login .']</OPTION>';
	}
	//

	//add categories
	$ct = NULL;
	list_categories(0, 0, 'select', 1, $ct, 0);
	//

	$array = array("function" => 'news_suggest', "id" => '', "date" => date('Y-m-d H:i:s'), "ldate" => date($conf['date_format'], strtotime(date('Y-m-d H:i:s'))), "select_author" => $select_author, "image" => '', "source_url" => 'http://', "source" => '', "cat" => 1, "categories" => $ct, "checked" => 'CHECKED');

	$multi = NULL;

	$langs = get_lang_list();
	$languages = get_languages_list();

	$i = 0;

	foreach($langs as $key => $l) {
		$i++;
		if($l==$_SESSION['lang_short']) {
			$icon = 'collapse';
			$style = 'block';
		}
		else {
			$icon = 'expand';
			$style = 'none';
		}

		$multi .= string_template(read_file('admin/themes/news_form_multi.php'), array('lang' => $l, 'language' => strtoupper($l), 'icon' => $GLOBALS['theme_path'].$GLOBALS['theme_img'][$icon], 'style' => $style, 'i' => $i));
		$t = "title_$l";
		$array[$t] = '';
		$t = "text_$l";
		$array[$t] = '';

		//recover
		if(ereg('module=error', $_SERVER['HTTP_REFERER'])) {
			$t = "title_$l";
			$array[$t] = $_SESSION['recover_'.$t];
			$t = "text_$l";
			$array[$t] = $_SESSION['recover_'.$t];   
		}
		//	
	}

	$content = string_template(read_file('admin/themes/news_form.php'), array('multi' => $multi));
	$content = string_template($content, $array);
}

//put $content to proper theme/file
$content=string_template(read_file('news_main.php'), array('theme_path'=>$GLOBALS['theme_path'], 'content'=>$content, 'page_link_news'=>$page_link_news, 'intro'=>$intro));

//generating menus (what, template)
$menu_left=menu('left', 'left');
$menu_right=menu('right', 'right');
$menu_top=menu('top', 'top');
//

//site_generation_time
$GLOBALS['time_end'] = microtime(1);
$site_generation_time=abs(round($GLOBALS['time_end']-$GLOBALS['time_start'], 3));
//

//creating template
$tpl=new tpl;
$tpl->cache(false); //turn off cache
$tpl->cache_php(false); //cache php
$tpl_cached=false;

//header
$tpl->create('header.php', array('title' => $GLOBALS['lang']['global']['site_title'], 'description' => $GLOBALS['lang']['global']['site_description'], 'keywords' => $GLOBALS['lang']['global']['site_keywords'], 'theme_path'=>$GLOBALS['theme_path'], 'menu_top' => $menu_top));
//body
$tpl->create('body.php', array('theme_path' => $GLOBALS['theme_path'], 'menu_top' => $menu_top, 'menu_left' => $menu_left, 'menu_right' => $menu_right, 'content' => $content));
//footer
$tpl->create('footer.php', array('site_generation_time' => string_template($GLOBALS['lang']['global']['site_generation_time'], array('site_generation_time'=>$site_generation_time, 'queries'=>$db->query_count())), 'theme_path'=>$GLOBALS['theme_path'], 'copyright' => $GLOBALS['lang']['global']['site_copyright']));
//parse
$tpl->parse();

?>
